#include "mex.h"
#include <math.h>

double summation(double ptr[],int term)
{
    int i;
    double sum=0;
    for(i=0; i<term; i++)
        sum += ptr[i];
    
    return sum; 
}

static void midpoint(double p[], double n[], int r, int c)
{
        int i,j,k;
        double dt=0.5/5000;

        for (j=0; j<c; j++)
		{
			for (i=0; i<51; i++)
            {
                p[j*51+i]=summation(&n[j*r],100*i+1)*dt-(n[j*r]+n[j*r+100*i+1])*dt/2.0;
            }
		}


}



//  static void variation(long Npaths)
//  {
//      int i;
//      for (i=0,i<Npaths,i++)
//
//  }


void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
        double *p, *n;
        int row,column;
        
        row=mxGetM(prhs[0]); //5001 
        column=mxGetN(prhs[0]); 

        plhs[0]=mxCreateDoubleMatrix(51,column,mxREAL);
        p=mxGetPr(plhs[0]);
        n=mxGetPr(prhs[0]);

        midpoint(p,n,row,column);


}
